import numpy as np
from fastmcp import FastMCP

matrix_mcp = FastMCP("Matrix_server")

@matrix_mcp.tool()
def add_matrices(matrix1: np.ndarray, matrix2: np.ndarray) -> np.ndarray:
    """
    Add two matrices.

    Parameters:
    matrix1 (np.ndarray): First input matrix.
    matrix2 (np.ndarray): Second input matrix.

    Returns:
    np.ndarray: Resultant matrix after addition.
    """
    assert matrix1.shape == matrix2.shape, "Matrices must have the same dimensions for addition."
    assert matrix1.dtype == matrix2.dtype, "Matrices must have the same data type for addition."
    return np.add(matrix1, matrix2)

@matrix_mcp.tool()
def subtract_matrices(matrix1, matrix2):
    """
    Subtract the second matrix from the first matrix.

    Parameters:
    matrix1 (np.ndarray): First input matrix.
    matrix2 (np.ndarray): Second input matrix.

    Returns:
    np.ndarray: Resultant matrix after subtraction.
    """
    assert matrix1.shape == matrix2.shape, "Matrices must have the same dimensions for subtraction."
    assert matrix1.dtype == matrix2.dtype, "Matrices must have the same data type for subtraction."
    return np.subtract(matrix1, matrix2)


def multiply_matrices(matrix1, matrix2):
    """
    Multiply two matrices.

    Parameters:
    matrix1 (np.ndarray): First input matrix.
    matrix2 (np.ndarray): Second input matrix.

    Returns:
    np.ndarray: Resultant matrix after multiplication.
    """
    return np.dot(matrix1, matrix2)


def divide_matrices(matrix1, matrix2):
    """
    Perform element-wise division of two matrices.

    Parameters:
    matrix1 (np.ndarray): First input matrix.
    matrix2 (np.ndarray): Second input matrix.

    Returns:
    np.ndarray: Resultant matrix after division.
    """
    try:
        return np.divide(matrix1, matrix2)
    except ZeroDivisionError:
        return "Error: Division by zero encountered."